package DAO;
/*
@author joker
@create 2022-03-09 15:43
*/

import javabean.userDetails;

import java.util.Date;
import java.util.List;

public class UserDetailsDAO extends BasicDAO<userDetails> {
    //1. 就有BasicDAO的所有方法
    //2. 根据业务需求,可以编写特有的的方法
    //这里是注册用户需要使用的DAO层方法
    public int RegisterUser(String userID, String password, String phone, String Email, String CarID1, int status) {
        Date date = new Date();//已测试,OK
        UserDetailsDAO userDetailsDAO = new UserDetailsDAO();

        String year = String.format("%tY", date);

        String month4 = String.format("%tm", date);

        String day = String.format("%te", date);
        String today = year + "-" + month4 + "-" + day;
        System.out.println(year + "-" + month4 + "-" + day);

        String sql = "insert into userdetails values (?,?,?,?,?,null,null,?,0,0,0,?)";
        int num = userDetailsDAO.update(sql, userID, password, phone, Email, CarID1, today, status);
        if (num != 0) {
            System.out.println("用户注册成功:userid = " + userID);
        }
        return num;
    }

    //Biz调用searchManager(),用于管理员登陆
    public userDetails searchManager(String userID, String password) {//已测试,OK
        String sql = "select * from userdetails where userID = ? and password = ? and status = 2";
        UserDetailsDAO userDetailsDAO = new UserDetailsDAO();
        userDetails aa = null;
        try {
            aa = userDetailsDAO.querySingle(sql, userDetails.class, userID, password);
        } catch (Exception e) {
            System.out.println("获取管理员账户异常");
        }
        return aa;
    }

    //用于普通用户登陆,查找有没有该普通用户
    public userDetails searchUser(String userID, String password) {//已测试,OK
        String sql = "select * from userdetails where userID = ? and password = ? and (status = 0 or status = 1)";
        UserDetailsDAO userDetailsDAO = new UserDetailsDAO();
        userDetails aa = null;
        try {
            aa = userDetailsDAO.querySingle(sql, userDetails.class, userID, password);
        } catch (Exception e) {
            System.out.println("获取用户账户异常");
        }

        return aa;
    }

    //获取目标用户的所有信息
    public userDetails getUserBY(String userID) {
        String sql = "select * from userdetails where userID = ?";
        UserDetailsDAO userDetailsDAO = new UserDetailsDAO();
        userDetails aa = null;
        try {
            aa = userDetailsDAO.querySingle(sql, userDetails.class, userID);
        } catch (Exception e) {
            System.out.println("获取目标用户账户异常");
        }
        return aa;
    }

    //普通用户模块方法
    /*1. 修改用户的密码*/
    public boolean ChangePassword(String newPsd, String UserID) {
        UserDetailsDAO userDetailsDAO = new UserDetailsDAO();
        String sql = "update userdetails set password = ? where userid = ?";
        int num = userDetailsDAO.update(sql, newPsd, UserID);
        if (num != 0) {
            System.out.println("修改密码成功,newPsd = " + newPsd);
            return true;
        } else {
            return false;
        }
    }

    /*2. 修改用户手机号*/
    public boolean ChangePhone(String newPhone, String UserID) {
        UserDetailsDAO userDetailsDAO = new UserDetailsDAO();
        String sql = "update userdetails set phone = ? where userid = ?";
        int num = userDetailsDAO.update(sql, newPhone, UserID);
        if (num != 0) {
            System.out.println("修改手机号成功 newPhone = " + newPhone);
            return true;
        } else {
            return false;
        }
    }

    /*3. 修改车牌号1*/
    public int ChangeCarID1(String newCarID, String UserID) {
        UserDetailsDAO userDetailsDAO = new UserDetailsDAO();
        String sql = "update userdetails set CarID1 = ? where userid = ?";
        int num = userDetailsDAO.update(sql, newCarID, UserID);
        if (num != 0) {
            System.out.println("更改成功,newCarID1 = " + newCarID + "UserID = " + UserID);
        }
        return num;
    }

    /*4. 修改车牌号2*/
    public int ChangeCarID2(String newCarID, String UserID) {
        UserDetailsDAO userDetailsDAO = new UserDetailsDAO();
        String sql = "update userdetails set CarID2 = ? where userid = ?";
        int num = userDetailsDAO.update(sql, newCarID, UserID);
        if (num != 0) {
            System.out.println("更改成功,newCarID2 = " + newCarID + "UserID = " + UserID);
        }
        return num;

    }

    /*4. 修改车牌号3*/
    public int ChangeCarID3(String newCarID, String UserID) {
        UserDetailsDAO userDetailsDAO = new UserDetailsDAO();
        String sql = "update userdetails set CarID3 = ? where userid = ?";
        int num = userDetailsDAO.update(sql, newCarID, UserID);
        if (num != 0) {
            System.out.println("更改成功,newCarID3 = " + newCarID + "UserID = " + UserID);
        }
        return num;

    }

    //5. 更改剩余次数
    public boolean UpdateDegrees(String newDegrees, String UserID) {
        UserDetailsDAO userDetailsDAO = new UserDetailsDAO();
        String sql = "update userdetails set remainingdegrees = ? where userid = ?";
        int num = userDetailsDAO.update(sql, newDegrees, UserID);
        if (num != 0) {
            System.out.println("更改成功,newDegrees = " + newDegrees + "UserID = " + UserID);
        }
        return num == 1;
    }

    //6. 更改剩余天数
    public boolean UpdateTimes(String newTimes, String UserID) {
        UserDetailsDAO userDetailsDAO = new UserDetailsDAO();
        String sql = "update userdetails set remainingdays = ? where userid = ?";
        int num = userDetailsDAO.update(sql, newTimes, UserID);
        if (num != 0) {
            System.out.println("更改剩余次数成功:newTimes = " + newTimes + "UserID = " + UserID);
            return true;
        } else {
            return false;
        }
    }

    //7. 更改余额
    public boolean UpdateBalance(String newBalance, String UserID) {
        UserDetailsDAO userDetailsDAO = new UserDetailsDAO();
        String sql = "update userdetails set accountbalance = ? where userid = ?";
        int num = userDetailsDAO.update(sql, newBalance, UserID);
        if (num != 0) {
            System.out.println("更改余额成功,newBalance = " + newBalance + "UserID = " + UserID);
            return true;
        } else {
            return false;
        }
    }

    //修改用户的Email,需要UserID,返回0则修改失败
    public int ChangeEmail(String newEmail, String UserID) {
        UserDetailsDAO userDetailsDAO = new UserDetailsDAO();
        String sql = "update userdetails set Email = ? where userid = ?";
        int num = userDetailsDAO.update(sql, newEmail, UserID);
        if (num != 0) {
            System.out.println("更改Email成功,newEmail = " + newEmail + "UserID = " + UserID);
        }
        return num;
    }
    /* *//*8. 充值功能,需要分为次卡充值,天数充值,并返回剩余天数和次数*//*
    public int TimeCharge(String userID,String Times){
        UserDetailsDAO userDetailsDAO = new UserDetailsDAO();
        userDetails user1 = userDetailsDAO.getUserBY(userID);//获得当前用户的信息
        int remainingDegrees = user1.getRemainingDegrees();
        remainingDegrees += Integer.parseInt(Times);//计算得到该用户充值过后的天数
        String sql = "update userdetails set remainingdegrees = ? where userid = ?";
        userDetailsDAO.update(sql,remainingDegrees,userID);
        return remainingDegrees;
    }

    public int DateCharge(String userID,String Dates){
        UserDetailsDAO userDetailsDAO = new UserDetailsDAO();
        userDetails user1 = userDetailsDAO.getUserBY(userID);//获得当前用户的信息
        int remainingDegrees = user1.getRemainingDays();
        remainingDegrees += Integer.parseInt(Dates);//计算得到该用户充值过后的天数
        String sql = "update userdetails set remainingdays = ? where userid = ?";
        userDetailsDAO.update(sql,remainingDegrees,userID);
        return remainingDegrees;
    }

    *//*9.退费功能,需要分为次卡退费,天数退费,并返回剩余天数和次数,校验逻辑在前端*//*
    public int TimeReturn(String userID,String Times){//需要userid和
        UserDetailsDAO userDetailsDAO = new UserDetailsDAO();
        userDetails user1 = userDetailsDAO.getUserBY(userID);//获得当前用户的信息
        int remainingDegrees = user1.getRemainingDegrees();
        remainingDegrees -= Integer.parseInt(Times);//计算得到该用户退费过后的次数
        if (remainingDegrees <=0)
        {
            remainingDegrees = 0;
        }
        String sql = "update userdetails set remainingdegrees = ? where userid = ?";
        userDetailsDAO.update(sql,remainingDegrees,userID);
        return remainingDegrees;
    }

    public int DateReturn(String userID,String Dates){
        UserDetailsDAO userDetailsDAO = new UserDetailsDAO();
        userDetails user1 = userDetailsDAO.getUserBY(userID);//获得当前用户的信息
        int remainingDays = user1.getRemainingDays();
        remainingDays -= Integer.parseInt(Dates);//计算得到该用户退费过后的天数
        if (remainingDays <=0){
            remainingDays = 0;
        }
        String sql = "update userdetails set remainingdays = ? where userid = ?";
        userDetailsDAO.update(sql,remainingDays,userID);
        return remainingDays;
    }*/


    /*10. 管理员模块*/
    // 返回所有普通用户信息
    public List<userDetails> getAllUsers() {
        UserDetailsDAO userDetailsDAO = new UserDetailsDAO();
        String sql = "select * from userdetails where status != 2";
        List<userDetails> aa = null;
        try {
            aa = userDetailsDAO.queryMultiply(sql, userDetails.class);
        } catch (Exception e) {
            System.out.println("获取所有用户账户异常");
        }
        if (!aa.equals(null)) {
            System.out.println("搜索所有用户成功");
        }
        return aa;
    }

}
